package com.read;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class LogParser {
    private static final String LOG_FILE_PATH = "/Users/yzw/Desktop/log-info-lat.log";
    private static final String JSON_FILE_PATH = "/Users/yzw/Desktop/json.json";
    private static final Pattern JSON_PATTERN = Pattern.compile("\\{.*?\\}");
    private static final Pattern TIMESTAMP_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}");

    public static void main(String[] args) {
        List<CommuteSearchRequest> requests = parseLog(LOG_FILE_PATH);
        generateNavicatJSONFile(requests, JSON_FILE_PATH);
    }

    public static List<CommuteSearchRequest> parseLog(String filePath) {
        List<CommuteSearchRequest> resultList = new ArrayList<>();
        Set<String> seenLocations = new HashSet<>();
        ObjectMapper objectMapper = new ObjectMapper();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                Matcher jsonMatcher = JSON_PATTERN.matcher(line);
                Matcher timeMatcher = TIMESTAMP_PATTERN.matcher(line);

                if (jsonMatcher.find()) {
                    String json = jsonMatcher.group();
                    String timestamp = timeMatcher.find() ? timeMatcher.group() : null;

                    try {
                        CommuteSearchRequest request = objectMapper.readValue(json, CommuteSearchRequest.class);
                        request.setTimestamp(timestamp);

                        // 经纬度去重逻辑
                        String locationKey = request.getLon() + "," + request.getLat();
                        if (seenLocations.contains(locationKey)) {
                            continue; // 已处理过的经纬度，跳过
                        }
                        seenLocations.add(locationKey);

                        resultList.add(request);
                    } catch (Exception e) {
                        System.err.println("JSON 解析失败: " + json);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultList;
    }

    public static void generateNavicatJSONFile(List<CommuteSearchRequest> requests, String outputPath) {
        ObjectMapper objectMapper = new ObjectMapper();
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath))) {
            writer.write("[");
            writer.newLine();

            for (int i = 0; i < requests.size(); i++) {
                String json = objectMapper.writeValueAsString(requests.get(i));
                writer.write(json);
                if (i < requests.size() - 1) {
                    writer.write(",");
                }
                writer.newLine();
            }

            writer.write("]");
            writer.newLine();
            System.out.println("Navicat JSON 数据已写入: " + outputPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
